<?php
    class Common
    {

        public static function pageDetail($total_page,$page,$link,$id,$alias)
        {
            $paging = "";
            $cur_page = $page;
            $back = 1;


            if($total_page<=7)
            {
                for($i=1;$i<=$total_page;$i++)
                {
                    $paging .= '<a id='. $i .' class="numbertrang" href="'.Yii::app()->createUrl($link,array('page'=>$i,'id'=>$id,'alias'=>$alias)).'">'.$i.'</a>&nbsp;&nbsp;';  
                }
            }
            else
            {
                if($cur_page<4)
                {
                    for($i=1;$i<=4;$i++)
                    {
                        $paging .= '<a id='. $i .' class="numbertrang" href="'.Yii::app()->createUrl($link,array('page'=>$i,'id'=>$id,'alias'=>$alias)).'">'.$i.'</a>&nbsp;&nbsp;'; 

                    }
                    $paging .= '... &nbsp;&nbsp';
                    for($i=$total_page-3;$i<=$total_page;$i++)
                    {
                        $paging .= '<a id='. $i .' class="numbertrang" href="'.Yii::app()->createUrl($link,array('page'=>$i,'id'=>$id,'alias'=>$alias)).'">'.$i.'</a>&nbsp;&nbsp;'; 
                    }  
                }
                if($cur_page>=4 and $cur_page<=$total_page-3)
                {
                    $paging .= '&nbsp;&nbsp ... &nbsp;&nbsp';
                    for($i=$cur_page-2;$i<=$cur_page+2;$i++)
                    {
                        $paging .= '<a id='. $i .' class="numbertrang" href="'.Yii::app()->createUrl($link,array('page'=>$i,'id'=>$id,'alias'=>$alias)).'">'.$i.'</a>&nbsp;&nbsp;';  
                    }
                    $paging .= '&nbsp;&nbsp ... &nbsp;&nbsp';
                }
                if($cur_page>($total_page-3))
                {
                    for($i=1;$i<=4;$i++)
                    {
                        $paging .= '<a id='. $i .' class="numbertrang" href="'.Yii::app()->createUrl($link,array('page'=>$i,'id'=>$id,'alias'=>$alias)).'">'.$i.'</a>&nbsp;&nbsp;';  
                    }
                    $paging .= '... &nbsp;&nbsp';
                    for($i=$total_page-3;$i<=$total_page;$i++)
                    {
                        $paging .= '<a id='. $i .' class="numbertrang" href="'.Yii::app()->createUrl($link,array('page'=>$i,'id'=>$id,'alias'=>$alias)).'">'.$i.'</a>&nbsp;&nbsp;';  
                    }
                }
            }

            if($total_page == 1)
            {
                $paging = "";
            }

            return $paging;

        }

        public static function pageHot($total_page,$page,$link)
        {
            $paging = "";
            $cur_page = $page;
            $back = 1;

            if($total_page<=7)
            {
                for($i=1;$i<=$total_page;$i++)
                {
                    $paging .= '<a id='. $i .' class="numbertrang" href="'.Yii::app()->createUrl($link,array('page'=>$i)).'">'.$i.'</a>&nbsp;&nbsp;';  
                }
            }
            else
            {
                if($cur_page<4)
                {
                    for($i=1;$i<=4;$i++)
                    {
                        $paging .= '<a id='. $i .' class="numbertrang" href="'.Yii::app()->createUrl($link,array('page'=>$i)).'">'.$i.'</a>&nbsp;&nbsp;'; 

                    }
                    $paging .= '... &nbsp;&nbsp';
                    for($i=$total_page-3;$i<=$total_page;$i++)
                    {
                        $paging .= '<a id='. $i .' class="numbertrang" href="'.Yii::app()->createUrl($link,array('page'=>$i)).'">'.$i.'</a>&nbsp;&nbsp;'; 
                    }  
                }
                if($cur_page>=4 and $cur_page<=$total_page-3)
                {
                    $paging .= '&nbsp;&nbsp ... &nbsp;&nbsp';
                    for($i=$cur_page-2;$i<=$cur_page+2;$i++)
                    {
                        $paging .= '<a id='. $i .' class="numbertrang" href="'.Yii::app()->createUrl($link,array('page'=>$i)).'">'.$i.'</a>&nbsp;&nbsp;';  
                    }
                    $paging .= '&nbsp;&nbsp ... &nbsp;&nbsp';
                }
                if($cur_page>($total_page-3))
                {
                    for($i=1;$i<=4;$i++)
                    {
                        $paging .= '<a id='. $i .' class="numbertrang" href="'.Yii::app()->createUrl($link,array('page'=>$i)).'">'.$i.'</a>&nbsp;&nbsp;';  
                    }
                    $paging .= '... &nbsp;&nbsp';
                    for($i=$total_page-3;$i<=$total_page;$i++)
                    {
                        $paging .= '<a id='. $i .' class="numbertrang" href="'.Yii::app()->createUrl($link,array('page'=>$i)).'">'.$i.'</a>&nbsp;&nbsp;';  
                    }
                }
            }

            if($total_page == 1)
            {
                $paging = "";
            }

            return $paging;

        }

        public static function pageSomo($total_page,$page)
        {
            $paging = "";
            $cur_page = $page;
            $back = 1;

            if($total_page<=7)
            {
                for($i=1;$i<=$total_page;$i++)
                {
                    $paging .= '<a id='. $i .' class="numbertrang" href="'.Yii::app()->createUrl('tienich/somo',array('page'=>$i)).'">'.$i.'</a>&nbsp;&nbsp;';  
                }
            }
            else
            {
                if($cur_page<4)
                {
                    for($i=1;$i<=4;$i++)
                    {
                        $paging .= '<a id='. $i .' class="numbertrang" href="'.Yii::app()->createUrl('tienich/somo',array('page'=>$i)).'">'.$i.'</a>&nbsp;&nbsp;'; 

                    }
                    $paging .= '... &nbsp;&nbsp';
                    for($i=$total_page-3;$i<=$total_page;$i++)
                    {
                        $paging .= '<a id='. $i .' class="numbertrang" href="'.Yii::app()->createUrl('tienich/somo',array('page'=>$i)).'">'.$i.'</a>&nbsp;&nbsp;'; 
                    }  
                }
                if($cur_page>=4 and $cur_page<=$total_page-3)
                {
                    $paging .= '&nbsp;&nbsp ... &nbsp;&nbsp';
                    for($i=$cur_page-2;$i<=$cur_page+2;$i++)
                    {
                        $paging .= '<a id='. $i .' class="numbertrang" href="'.Yii::app()->createUrl('tienich/somo',array('page'=>$i)).'">'.$i.'</a>&nbsp;&nbsp;';  
                    }
                    $paging .= '&nbsp;&nbsp ... &nbsp;&nbsp';
                }
                if($cur_page>($total_page-3))
                {
                    for($i=1;$i<=4;$i++)
                    {
                        $paging .= '<a id='. $i .' class="numbertrang" href="'.Yii::app()->createUrl('tienich/somo',array('page'=>$i)).'">'.$i.'</a>&nbsp;&nbsp;';  
                    }
                    $paging .= '... &nbsp;&nbsp';
                    for($i=$total_page-3;$i<=$total_page;$i++)
                    {
                        $paging .= '<a id='. $i .' class="numbertrang" href="'.Yii::app()->createUrl('tienich/somo',array('page'=>$i)).'">'.$i.'</a>&nbsp;&nbsp;';  
                    }
                }
            }

            return $paging;
        }

        public static function pageSearch($total_page,$page,$search)
        {
            $paging = "";
            $cur_page = $page;
            $back = 1;
            $pun = "'";

            if($total_page<=7)
            {
                for($i=1;$i<=$total_page;$i++)
                {
                    $paging .= '<a id='. $i .' class="numbertrang" onclick="pageSearch('. $i .','.$pun.''. $search .''.$pun.');" href="javascript:void(0);">'.$i.'</a>&nbsp;&nbsp;';  
                }
            }
            else
            {
                if($cur_page<4)
                {
                    for($i=1;$i<=4;$i++)
                    {
                        $paging .= '<a id='. $i .' class="numbertrang" onclick="pageSearch('. $i .','.$pun.''. $search .''.$pun.');" href="javascript:void(0);">'.$i.'</a>&nbsp;&nbsp;';   

                    }
                    $paging .= '... &nbsp;&nbsp';
                    for($i=$total_page-3;$i<=$total_page;$i++)
                    {
                        $paging .= '<a id='. $i .' class="numbertrang" onclick="pageSearch('. $i .','.$pun.''. $search .''.$pun.');" href="javascript:void(0);">'.$i.'</a>&nbsp;&nbsp;';     
                    }  
                }
                if($cur_page>=4 and $cur_page<=$total_page-3)
                {
                    $paging .= '&nbsp;&nbsp ... &nbsp;&nbsp';
                    for($i=$cur_page-2;$i<=$cur_page+2;$i++)
                    {
                        $paging .= '<a id='. $i .' class="numbertrang" onclick="pageSearch('. $i .','.$pun.''. $search .''.$pun.');" href="javascript:void(0);">'.$i.'</a>&nbsp;&nbsp;';    
                    }
                    $paging .= '&nbsp;&nbsp ... &nbsp;&nbsp';
                }
                if($cur_page>($total_page-3))
                {
                    for($i=1;$i<=4;$i++)
                    {
                        $paging .= '<a id='. $i .' class="numbertrang" onclick="pageSearch('. $i .','.$pun.''. $search .''.$pun.');" href="javascript:void(0);">'.$i.'</a>&nbsp;&nbsp;';      
                    }
                    $paging .= '... &nbsp;&nbsp';
                    for($i=$total_page-3;$i<=$total_page;$i++)
                    {
                        $paging .= '<a id='. $i .' class="numbertrang" onclick="pageSearch('. $i .','.$pun.''. $search .''.$pun.');" href="javascript:void(0);">'.$i.'</a>&nbsp;&nbsp;';      
                    }
                }
            }

            return $paging;
        }

        public static function  getInnerSubstring($string,$delim)
        {
            $string = explode($delim, $string, 3); 
            var_dump($string);
        }

        public static function cleanQuery($string)
        { 
            if(empty($string)) return $string;
            $string = mysql_real_escape_string(trim($string));
            $badWords = array(
                "/Select(.*)From/i"
                , "/Union(.*)Select/i"
                , "/Update(.*)Set/i"
                , "/Delete(.*)From/i"
                , "/Drop(.*)Table/i"
                , "/Insert(.*)Into/i" 
                , "/http/i"
                , "/--/i"
            );
            $string = preg_replace($badWords, "",$string);
            return $string;
        }

    }
?>
